package com.zdzx.common.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Lists;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	//@Value("${spring.profiles.active}")
	private  String active="dev";
	@Bean
	public Docket api1() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("admin").enable(true).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.zdzx.controller.admin")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());

	}
	
	@Bean
	public Docket api2() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("web").enable(true).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.zdzx.controller.web")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());

	}

	private List<SecurityScheme> securitySchemes() {
		SecurityScheme ss=new ApiKey("Authorization", "Authorization", "header");
		return Lists.newArrayList(ss);
	}

	@SuppressWarnings("deprecation")
	private List<SecurityContext> securityContexts() {
		return Lists.newArrayList(SecurityContext.builder().securityReferences(defaultAuth())
				.forPaths(PathSelectors.regex("^(?!auth).*$")).build());
	}

	List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
	}

	// -------------

	/*
	 * @Bean public Docket createRestApi() { return new
	 * Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
	 * .apis(RequestHandlerSelectors.basePackage("com.sxmpx.controller"))
	 * .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
	 * .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths
	 * (PathSelectors.any()) .build(); }
	 */

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("API") // System.getProperty("user.dir")
				.description(active+"文档说明").version("2.0").build();
	}

	/*
	 * @Bean UiConfiguration uiConfig() { return new UiConfiguration(null, "list",
	 * "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false,
	 * true, 60000L); }
	 */

	/*
	 * private Swagger extend(Swagger swagger) { // 响应返回参数增强
	 * Iterator<Map.Entry<String, Model>> it =
	 * swagger.getDefinitions().entrySet().iterator(); while (it.hasNext()) {
	 * Map.Entry<String, Model> entry = it.next(); Model model = entry.getValue();
	 * String key = entry.getKey(); if (key.contains("JsonResult")) { Map<String,
	 * Property> props = model.getProperties(); Property dataProp =
	 * props.get("data"); Property newProp = SwaggerUtil.getNewProp(dataProp,
	 * SwaggerUtil.getRealType(key), swagger.getDefinitions()); props.put("data",
	 * newProp); } } return swagger; }
	 */
}
